﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Wandering
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)     //注册
        {
            var uname = Uname.Text;
            var pword = Pword.Text;
            var repword = RePword.Text;

            if(Uname.Text !="" && Pword.Text !="" && RePword.Text != "")
            {
                if(pword == repword)
                {
                    var sql = string.Format("select * from Users where Username='{0}' and Password='{1}'", uname, pword);

                    var dt = DBHelper.GetDataTable(sql);

                    if (dt.Rows.Count == 1)
                    {
                        MessageBox.Show("该用户名已经存在！");
                    }
                    else
                    {
                        var sql2 = string.Format("insert into Users(Username,Password) values('{0}','{1}')", uname, pword);

                        var x = DBHelper.InsertOrDeleteOrUpdate(sql2);

                        if(x >= 1)
                        {
                            MessageBox.Show("注册成功! ");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("注册失败！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("两次输入的密码不一致，请重新输入！");
                }
            }
            else
            {
                MessageBox.Show("注册内容不能为空,请重试！");
            }

        }

        private void button2_Click(object sender, EventArgs e)     //取消
        {
            this.Close();
        }
    }
}
